package com.situ.stmall.manger.controller.api;
import com.situ.stmall.manger.entity.Result;
import com.situ.stmall.manger.util.UploadUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/upload")
public class APIUploadController {
    //使用spring的EL表达式，读取文件中upload.path属性的值
    @Value("${upload.path}")
    private  String path;
    /*
    上传文件用post请求
    * */
    @PostMapping
    public Result upload(MultipartFile file) {
        String fileName = UploadUtil.save(file, path);//不能把路径写死
        if (fileName != null) {
            return Result.success(fileName);
        } else {
            return Result.error("上传失败");
        }
    }
}
